#include <iostream>

using namespace std;

/*
 * 英雄结构体，包括姓名，年龄，性别，创建结构体数组，存放5名英雄，通过冒泡排序，将数组中英雄按照年龄进行升序排序
 */

struct Hero{
    string name;
    int age;
    string sex;
};

void bubbleSort(struct Hero hArray[], int length) {
    for (int i = 0; i < length-1; ++i) {
        for (int j = 0; j < length-1-i; ++j) {
            if(hArray[j].age > hArray[j+1].age) {
                string tempName = hArray[j].name;
                int tempAge = hArray[j].age;
                string tempSex = hArray[j].sex;
                hArray[j].name = hArray[j+1].name;
                hArray[j].age = hArray[j+1].age;
                hArray[j].sex = hArray[j+1].sex;
                hArray[j+1].name = tempName;
                hArray[j+1].age = tempAge;
                hArray[j+1].sex = tempSex;
            }
        }
    }
}

int main() {
    struct Hero h1 = {"刘备", 23, "男"};
    struct Hero h2 = {"关羽", 22, "男"};
    struct Hero h3 = {"张飞", 20, "男"};
    struct Hero h4 = {"赵云", 21, "男"};
    struct Hero h5 = {"貂蝉", 19, "女"};
    struct Hero hArray[] = {h1, h2, h3, h4, h5};
    bubbleSort(hArray, 5);
    for (int i = 0; i < 5; ++i) {
        cout << hArray[i].name << "; " << hArray[i].age << "; " << hArray[i].sex << endl;
    }

    return 0;
}
